//
//  CheekUserDetailedView.m
//  CheekiOS
//
//  Created by mac on 2017/7/10.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "CheekUserDetailedView.h"

@implementation CheekUserDetailedView


- (instancetype)init {
    self = [super init];
    if (self) {
        [self addSubViews];
        
        
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubViews];
        
        
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self addSubViews];
        
        
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

-(void)addSubViews{

    _shieldingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_shieldingBtn setImage:[UIImage imageNamed:@"viedo_data_defriend_icon"] forState:UIControlStateNormal];
    [_shieldingBtn setTitle:@"拉黑" forState:UIControlStateNormal];
    [_shieldingBtn setTitleColor:ColorI(0x2e353f) forState:UIControlStateNormal];
    _shieldingBtn.titleLabel.font = FontP(12);
    [_shieldingBtn addTarget:self action:@selector(shieldingBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_shieldingBtn];

    
    
    
    self.headerView = [[UIImageView alloc]init];
    self.headerView.contentMode = UIViewContentModeScaleAspectFill;
    
    self.headerView.layer.masksToBounds =YES;
    
    CALayer * layer = [self.headerView layer];
    layer.borderColor = Colorbackground.CGColor;
    layer.borderWidth = 1.0f;
    [self addSubview:self.headerView];

    _nameLab = [[UILabel alloc] init];
    _nameLab.textColor = [UIColor blackColor];
    _nameLab.font = FontP(17);
    _nameLab.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_nameLab];
    
    
    
    self.ageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:self.ageBtn];
    self.ageBtn.enabled = NO;
    self.ageBtn.layer.cornerRadius = 4;
    self.ageBtn.layer.masksToBounds = YES;
    [self.ageBtn setTitleColor:Theme_Color_White forState:UIControlStateNormal];
    self.ageBtn .imageEdgeInsets = UIEdgeInsetsMake(0,-5,0,0);//设置image在button上的位置（上top，左left，下bottom，右right）这里可以写负值，对上写－5，那么image就象上移动5个像素
    self.ageBtn.titleEdgeInsets = UIEdgeInsetsMake(0,0,0,0);
   
    self.constellationLab = [[UILabel alloc] init];
    [self addSubview:self.constellationLab];
    _constellationLab.textColor = Theme_Color_White;
    _constellationLab.layer.cornerRadius = 4;
    _constellationLab.layer.masksToBounds = YES;
    _constellationLab.backgroundColor = RandomColor;
    _constellationLab.textAlignment = NSTextAlignmentCenter;
    self.constellationLab.font = FontP(12);
    self.ageBtn.titleLabel.font = FontBold(12);

    
    self.detaileLab = [[UILabel alloc] init];
    self.detaileLab.textColor = ColorI(0x2e353f);
    self.detaileLab.font = FontP(12);
    self.detaileLab.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.detaileLab];
    
    self.sigLab = [[UILabel alloc] init];
    self.sigLab.textColor = ColorI(0x2e353f);
    self.sigLab.font = FontP(12);
    self.sigLab.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.sigLab];
    
    
    self.ShutdownBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.ShutdownBtn setTitle:@"关闭" forState:UIControlStateNormal];
    [self.ShutdownBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.ShutdownBtn addTarget:self action:@selector(ShutdownBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_ShutdownBtn];
    
    

}





-(void)setModel:(CheekVodeModel *)model{
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight | UIRectCornerTopRight | UIRectCornerTopLeft   cornerRadii:CGSizeMake(10, 10)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
    
   NSArray * arr = [[NSArray alloc] init];

    if (model.skill.length) {
        
        arr = [model.skill componentsSeparatedByString:@","];
    }

    
    [_shieldingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(15);
        make.left.equalTo(self).offset(15);
        make.height.mas_offset(25*SCREEN_WIDTH_RATIO47);
    }];
    
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(44*SCREEN_WIDTH_RATIO47);
        make.width.height.mas_offset(60*SCREEN_WIDTH_RATIO47);
        make.left.equalTo(self.mas_centerX).offset(-30*SCREEN_WIDTH_RATIO47);
    }];
    
    self.headerView.layer.cornerRadius =(30*SCREEN_WIDTH_RATIO47);

    
    
    [_nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_headerView.mas_bottom).offset(7.5*SCREEN_WIDTH_RATIO47);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(30*SCREEN_WIDTH_RATIO47);
    }];
    
    [_ageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_nameLab.mas_bottom).offset(5*SCREEN_WIDTH_RATIO47);
        make.width.mas_equalTo(50*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(20*SCREEN_WIDTH_RATIO47);
        make.centerX.equalTo(self).offset(-26);
    }];
    
    
    [_constellationLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_nameLab.mas_bottom).offset(5*SCREEN_WIDTH_RATIO47);
        make.width.mas_equalTo(50*SCREEN_WIDTH_RATIO47);
        make.height.mas_equalTo(20*SCREEN_WIDTH_RATIO47);
        make.left.equalTo(_ageBtn.mas_right).offset(1);
    }];
    
    [_detaileLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_constellationLab.mas_bottom).offset(5*SCREEN_WIDTH_RATIO47);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(25*SCREEN_WIDTH_RATIO47);
    }];
    
    [_sigLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_detaileLab.mas_bottom).offset(5*SCREEN_WIDTH_RATIO47);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(25*SCREEN_WIDTH_RATIO47);
    }];
    
    [self.ShutdownBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom);
        make.left.right.equalTo(self);
        make.height.mas_equalTo(44*SCREEN_WIDTH_RATIO47);
    }];
    
    UILabel *linlab = [[UILabel alloc] init];
    linlab.backgroundColor = RGB(246, 246, 246);
    [self addSubview:linlab];
    [linlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.left.width.equalTo(self);
        make.bottom.equalTo(self.ShutdownBtn.mas_top);
    }];
    
    
    
    [_headerView sd_setImageWithURL:[NSURL URLWithString:HttpImgUrl(model.photo)] placeholderImage:[UIImage imageNamed:@"llustration"]];
    
    _nameLab.text = model.nickName;
    
    _detaileLab.text = [NSString stringWithFormat:@"ID:%@ | %@ | %@km",model.uid,model.location,model.distance];
    
    _sigLab.text = model.motto;
    
    if (model.sex == 0) {
        [self.ageBtn setBackgroundColor:ColorI(0xf28484)];

        [self.ageBtn setImage:[UIImage imageNamed:@"female"] forState:UIControlStateNormal];
    }else{
        [self.ageBtn setBackgroundColor:ColorI(0x84b9f2)];

        [self.ageBtn setImage:[UIImage imageNamed:@"male"] forState:UIControlStateNormal];
    }
    //年龄
    [self.ageBtn setTitle:model.age  forState:UIControlStateNormal];
    //星座
    self.constellationLab.backgroundColor = [self getColorWithStr:model.star];
    
    self.constellationLab.text = model.star;
    
    //间距
    CGFloat IUW = 25;
    //当前最大的x
    CGFloat maxX = 0;
    NSArray * colorImgArr = @[@"boy_blue",@"green",@"orange",@"purple"];
    for (int i = 0; i < arr.count ; i++) {
        UIImageView *userImageColor = [[UIImageView alloc] init];
        UILabel *userLab = [[UILabel alloc] init];
        userLab.textColor = ColorI(0x992e353f);
        userLab.font = FontP(12);
        userLab.text = arr[i];
        
        
        CGSize userLabSize = textSizeFont(userLab.text,userLab.font, KScreenWidth, 40);
        userImageColor.frame = CGRectMake(maxX+IUW, self.height-73*SCREEN_WIDTH_RATIO47, 10, 10);
        userLab.frame = CGRectMake(userImageColor.right+5,  self.height-85.5*SCREEN_WIDTH_RATIO47, userLabSize.width, 40);
        userImageColor.image = [UIImage imageNamed:colorImgArr[i%colorImgArr.count]];
        
        [self addSubview:userImageColor];
        [self addSubview:userLab];
        maxX = CGRectGetMaxX(userLab.frame) ;
    }
    
    
}




- (UIColor *)getColorWithStr:(NSString *)starStr{
    
    NSArray * starArr = @[@"白羊座",@"天秤座",@"金牛座",@"天蝎座",@"双子座",@"射手座",@"巨蟹座",@"摩羯座",@"狮子座",@"处女座",@"双鱼座"];
    NSArray * colorArr = @[ColorI(0xd80808),ColorI(0xec3db0),ColorI(0x2898df),ColorI(0xa651c9),ColorI(0xff6839),ColorI(0x77ca69),ColorI(0xd16739),ColorI(0x30d3d5),ColorI(0x19d7ad),ColorI(0xecac3d),ColorI(0x32b576)];
    
    
    for (NSString * star in starArr) {
        if ([star isEqualToString:starStr]) {
            NSInteger index = [starArr indexOfObject:star];
            if (colorArr[index]) {
                return colorArr[index];
            }
        }
    }
    return ColorI(0xd80808);
}



- (void)shieldingBtnClick:(UIButton *)sender{


    if ([self.delegate respondsToSelector:@selector(shieldingBtnClick:)]) {
        [self.delegate shieldingBtnClick:sender];
    }


}
-(void)ShutdownBtnClick:(UIButton *)sender{

    if ([self.delegate respondsToSelector:@selector(ShutdownBtnClick:)]) {
        [self.delegate ShutdownBtnClick:sender];
    }
}
@end
